package main

import (
	"encoding/json"
	"fmt"
)

type Movie struct {
	Title  string   `json:"title"` //标签
	Year   int      `json:"year"` //首字母为空的话 序列化为空
	Price  int      `json:"rmb"`
	Actors []string `json:"actors"`
}

func main() {
	movie := Movie{
		Title:  "hh",
		Year:   2020,
		Price:  34,
		Actors: []string{"ronghao,jj"},
	}
	//编码的过程  结构体---> json
	jsonStr, err := json.Marshal(movie)
	if err != nil {
		fmt.Println("json marshal error", err)
		return
	}
	//{"title":"hh","year":2020,"rmb":34,"actors":["ronghao,jj"]}
	fmt.Printf("jsonStr = %s\n", jsonStr)

	//解码的过程 jsonstr ---> 结构体
	//jsonStr = {"title":"喜剧之王","year":2000,"rmb":10,"actors":["xingye","zhangbozhi"]}

	m := Movie{}
	err = json.Unmarshal(jsonStr, &m)
	if err != nil {
		fmt.Println("json unmarshal error ", err)
		return
	}
	fmt.Printf("%v\n", m)
}
